/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.skycode.tuto.manager;

import com.skycode.cnx.Conexion;
import com.skycode.tuto.dto.MonitorDto;
import com.skycode.tuto.dto.MonitorPdfDto;
import com.skycode.tuto.dto.PdfCommentDto;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 *
 * @author LumiLatop
 */
public class MonitorPdfManager implements Serializable {

    public MonitorPdfDto inicializar() {
        MonitorPdfDto dto = new MonitorPdfDto();
        dto.setMonitorDto(new MonitorDto());
        dto.setNombre("");
        dto.setUrlPdf("");
        dto.setFechaCreacion(obtenerFechaActual());
        dto.setListaComentariosPdf(new ArrayList<PdfCommentDto>());
        
        return dto;
    }

    public List<MonitorPdfDto> listarTodos() {
        List<MonitorPdfDto> lista = new ArrayList<MonitorPdfDto>();
        try {
            Connection cnx = Conexion.getConexionOracle();
            String query = "select * from JTUTO_MONITOR_PDF";
            PreparedStatement ps = cnx.prepareStatement(query);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                MonitorPdfDto monitorPdfDto = new MonitorPdfDto();
                monitorPdfDto.setId(rs.getLong(1));
                monitorPdfDto.setMonitorDto(obtenerMonitor(rs.getLong(2)));
                monitorPdfDto.setNombre(rs.getString(3));
                monitorPdfDto.setUrlPdf(rs.getString(4));
                monitorPdfDto.setFechaCreacion(rs.getString(5));
                monitorPdfDto.setListaComentariosPdf(obtenerListaComentarios(monitorPdfDto.getId()));
                lista.add(monitorPdfDto);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return lista;
    }
    
    private List<PdfCommentDto> obtenerListaComentarios(Long id) {
        List<PdfCommentDto> lista = new ArrayList<PdfCommentDto>();
        PdfCommentManager pdfManager = new PdfCommentManager();
        lista = pdfManager.obtenerTodosPorId(id);
        
        return lista;
    }

    private MonitorDto obtenerMonitor(Long id) {
        MonitorManager monitorManager = new MonitorManager();
        MonitorDto monitorDto = new MonitorDto();
        monitorDto = monitorManager.obtenerPorIdNoLista(id);

        return monitorDto;
    }
    
    public MonitorPdfDto obtenerPorIdNoLista(Long id) {
        MonitorPdfDto monitorPdfDto = new MonitorPdfDto();
        try {
            Connection cnx = Conexion.getConexionOracle();
            String query = "select * from JTUTO_MONITOR_PDF where ID = " + id;
            PreparedStatement ps = cnx.prepareStatement(query);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                monitorPdfDto.setId(rs.getLong(1));
                monitorPdfDto.setMonitorDto(obtenerMonitor(rs.getLong(2)));
                monitorPdfDto.setNombre(rs.getString(3));
                monitorPdfDto.setUrlPdf(rs.getString(4));
                monitorPdfDto.setFechaCreacion(rs.getString(5));              
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return monitorPdfDto;
    }

    public void guardar(MonitorPdfDto dto) {
        try {
            Connection cnx = Conexion.getConexionOracle();
            String query = "INSERT INTO JTUTO_MONITOR_PDF VALUES (?,?,?,?,?)";
            PreparedStatement ps = cnx.prepareStatement(query);
            ps.setLong(1, generarId(dto));
            ps.setLong(2, dto.getMonitorDto().getId());
            ps.setString(3, dto.getNombre());
            ps.setString(4, dto.getUrlPdf());
            ps.setString(5, dto.getFechaCreacion());
            ps.executeQuery();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Long generarId(MonitorPdfDto dto) {
        Long maxId = 0L;
        List<MonitorPdfDto> lista = listarTodos();
        if (lista != null && lista.size() > 0) {
            for (MonitorPdfDto x : lista) {
                if (x.getId().compareTo(maxId) > 0) {
                    maxId = x.getId();
                }
            }
            maxId = maxId + 1;
        } else {
            maxId = 100L;
        }
        return maxId;
    }
    
    public String obtenerFechaActual() {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
        return sdf.format(calendar.getTime());
    }

    public List<MonitorPdfDto> obtenerTodosPorId(Long id) {
        List<MonitorPdfDto> lista = new ArrayList<MonitorPdfDto>();
        try {
            Connection cnx = Conexion.getConexionOracle();
            String query = "select * from JTUTO_MONITOR_PDF where MONITOR = " + id;
            PreparedStatement ps = cnx.prepareStatement(query);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                MonitorPdfDto monitorPdfDto = new MonitorPdfDto();
                monitorPdfDto.setId(rs.getLong(1));
                monitorPdfDto.setMonitorDto(obtenerMonitor(rs.getLong(2)));
                monitorPdfDto.setNombre(rs.getString(3));
                monitorPdfDto.setUrlPdf(rs.getString(4));
                monitorPdfDto.setFechaCreacion(rs.getString(5));
                lista.add(monitorPdfDto);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return lista;
    }
}
